package com.weitu.threeSimultaneities.safe.controller;

/**
 * Created by WT on 2017/4/25.
 */

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.threeSimultaneities.projectApproval.entity.ProjectApproval;
import com.weitu.threeSimultaneities.projectApproval.entity.ProjectSpecialty;
import com.weitu.threeSimultaneities.projectApproval.service.ProjectApprovalService;
import com.weitu.threeSimultaneities.safe.entity.*;
import com.weitu.threeSimultaneities.safe.service.*;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

@Controller
@RequestMapping(value = "threeSimultaneities/safe")
public class SafeController extends BaseController {
    @Autowired
    private SafeService safeService;
    @Autowired
    private ProjectApprovalService projectApprovalService;
    @Autowired
    private AccessoryUtilService accessoryUtilService;
    @Autowired//可研阶段
    private SafeFSDService safeFSDService;
    @Autowired//基础设计阶段
    private SafeDesignService safeDesignService;
    @Autowired//试运行阶段
    private SafeCommissioningService safeCommissioningService;
    @Autowired//验收阶段
    private SafeAcceptanceService safeAcceptanceService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return BASE_JSP_PATH + "threeSimultaneities/safe/list";
    }
    @RequestMapping(value = "/h/list", method = RequestMethod.GET)
    public String hlist() {
        return BASE_JSP_PATH + "threeSimultaneities/occHealth/list";
    }
    @RequestMapping(value = "/f/list", method = RequestMethod.GET)
    public String flist() {
        return BASE_JSP_PATH + "threeSimultaneities/fire/list";
    }

    /**
     * 获取分页数据
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        putParametersIntoMap(map);
        //查询数据总数
        int count =safeService.selectCount(map);
        //获取分页对象
        DataGridPagination Pagination = DataGridPagination.getInstance(request, count);
        PageView pageView = Pagination.getPageView();//生成pageView分页对象

        map.put("pageView", pageView);
        List<Safe> dataList = safeService.selectByParam(map);
        return Pagination.flushDataGridData(dataList);
    }

    /**
     * 职业卫生三同时
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getListH", method = RequestMethod.POST)
    public Object listAjaxH(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        putParametersIntoMap(map);
        //查询数据总数
        int count =safeService.selectCountH(map);
        //获取分页对象
        DataGridPagination Pagination = DataGridPagination.getInstance(request, count);
        PageView pageView = Pagination.getPageView();//生成pageView分页对象

        map.put("pageView", pageView);
        List<Safe> dataList = safeService.selectByParamH(map);
        return Pagination.flushDataGridData(dataList);
    }

    /**
     * 消防三同时
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getListF", method = RequestMethod.POST)
    public Object listAjaxF(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        putParametersIntoMap(map);
        //查询数据总数
        int count =safeService.selectCountF(map);
        //获取分页对象
        DataGridPagination Pagination = DataGridPagination.getInstance(request, count);
        PageView pageView = Pagination.getPageView();//生成pageView分页对象

        map.put("pageView", pageView);
        List<Safe> dataList = safeService.selectByParamF(map);
        return Pagination.flushDataGridData(dataList);
    }
    /**
     * 打开编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editAjax(Model model, String id,String projectId,String type){
        //安全专业主数据
        Safe safe=safeService.selectByPrimaryKey(id);
        //主项目信息
        ProjectApproval projectApproval=projectApprovalService.selectByProjectId(projectId);
        //对项目专业ID进行拼接
        if(projectApproval.getProjectSpecialtyList().size()>0){
            String SpecialtyList="";
            for(ProjectSpecialty projectSpecialty:projectApproval.getProjectSpecialtyList()){
                SpecialtyList+=","+projectSpecialty.getSpecialtyId();
            }
            SpecialtyList=SpecialtyList.substring(1);
            model.addAttribute("SpecialtyList",SpecialtyList);
        }
        //项目主信息附件
        List<AccessoryPackageInfo> proAccessoryList = accessoryUtilService.queryAccessoryByBusinessId(projectApproval.getId().toString());
        //安全专业所有附件
        List<AccessoryPackageInfo> safeAccessoryList = accessoryUtilService.queryAccessoryByBusinessId(id);
        //安全专业-可研阶段
        SafeFSD safeFSD= safeFSDService.selectBySubjectId(id);
        if(safeFSD!=null){ model.addAttribute("safeFSD", safeFSD);}
        //安全专业-基础设计阶段
        SafeDesign safeDesign=safeDesignService.selectBySubjectId(id);
        if(safeDesign!=null){model.addAttribute("safeDesign",safeDesign);}
       //安全专业-试运行设计阶段s
        SafeCommissioning safeCommissioning=safeCommissioningService.selectBySubjectId(id);
        if(safeDesign!=null){model.addAttribute("safeCommissioning",safeCommissioning);}

        //验收阶段
        SafeAcceptance safeAcceptance=safeAcceptanceService.selectBySubjectId(id);
        if(safeAcceptance!=null){model.addAttribute("safeAcceptance",safeAcceptance);}

        model.addAttribute("safe", safe);
        model.addAttribute("safeAccessoryList", safeAccessoryList);
        model.addAttribute("proAccessoryList", proAccessoryList);
        model.addAttribute("projectApproval", projectApproval);
        return BASE_JSP_PATH + "threeSimultaneities/safe/"+type;
    }
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String infoAjax(Model model, String id,String projectId){
        //主项目信息
        ProjectApproval projectApproval=projectApprovalService.selectByProjectId(projectId);
        //对项目专业ID进行拼接
        if(projectApproval.getProjectSpecialtyList().size()>0){
            String SpecialtyList="";
            for(ProjectSpecialty projectSpecialty:projectApproval.getProjectSpecialtyList()){
                SpecialtyList+=","+projectSpecialty.getSpecialtyId();
            }
            SpecialtyList=SpecialtyList.substring(1);
            model.addAttribute("SpecialtyList",SpecialtyList);
        }

        model.addAttribute("projectApproval", projectApproval);
        if ("h".equals(id)){
            return BASE_JSP_PATH + "threeSimultaneities/occHealth/edit";
        }else {
            return BASE_JSP_PATH + "threeSimultaneities/fire/edit";
        }

    }

    /***
     * 删除项目
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    @Transactional(readOnly = false)
    @SystemLog(module = "安全三同时", methods = "删除安全专业三同时信息")
    public AjaxResult deleteById(String id) {
        int boo = safeService.deleteByPrimaryKey(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("deleteH")
    @Transactional(readOnly = false)
    public AjaxResult deleteByIdH(String id) {
        int boo = safeService.deleteByPrimaryKeyH(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("deleteF")
    @Transactional(readOnly = false)
    public AjaxResult deleteByIdF(String id) {
        int boo = safeService.deleteByPrimaryKeyF(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }
}
